package com.bkitmobile.kesu.android.GUI;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bkitmobile.kesu.android.connectServer.staticClass;

public class ChoiceDialog extends Dialog implements OnClickListener, android.view.View.OnKeyListener{
	private ChoiceInfo[] listInfos;
	private Button btnSelectAll, btnSelectNone, btnToggle, btnSave, btnCancel, btnReset;
	private GridView grid;
	private Context context;
	private boolean []store;
	
	public ChoiceDialog(Context context) {
		super(context);
		this.context = context;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.setting_tab_general_choice);
		
		btnSelectAll = (Button) findViewById(R.id.setting_tab_general_selectAll);
		btnSelectNone = (Button) findViewById(R.id.setting_tab_general_selectNone);
		btnToggle = (Button) findViewById(R.id.setting_tab_general_Toggle);
		btnSave = (Button) findViewById(R.id.setting_tab_general_save);
		btnCancel = (Button) findViewById(R.id.setting_tab_general_cancel);
		btnReset = (Button) findViewById(R.id.setting_tab_general_reset);
		btnSelectAll.setOnClickListener(this);
		btnSelectNone.setOnClickListener(this);
		btnToggle.setOnClickListener(this);
		btnSave.setOnClickListener(this);
		btnReset.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
		
		grid = (GridView) findViewById(R.id.setting_tab_general_grid);
		grid.setOnItemClickListener(new GridItemClickListener());
		
	}
	public GridView getGrid()
	{
		return grid;
	}
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	public void show(ChoiceInfo []listInfos) {
		if(listInfos != null){
			this.listInfos = listInfos;
			
		}
		backupState();
		
		grid.setAdapter(new ChoiceAdapter(context, this.listInfos));
		super.show();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.setting_tab_general_selectAll:
			((ChoiceAdapter)grid.getAdapter()).checkAll();
			break;
		case R.id.setting_tab_general_selectNone:
			((ChoiceAdapter)grid.getAdapter()).checkNone();
			break;
		case R.id.setting_tab_general_Toggle:
			((ChoiceAdapter)grid.getAdapter()).toggleAll();
			break;
		case R.id.setting_tab_general_save:
			dismiss();
			break;
		case R.id.setting_tab_general_reset:
			((ChoiceAdapter)grid.getAdapter()).reset(store);
			break;
		case R.id.setting_tab_general_cancel:
			((ChoiceAdapter)grid.getAdapter()).reset(store);
			dismiss();
			break;
		default:
			break;
		}
		
	}
	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			((ChoiceAdapter)grid.getAdapter()).reset(store);
			dismiss();
		}
		return false;
	}
	private void backupState(){
		int length = listInfos.length;
		if(store == null || store.length < length)
			store = new boolean[length];
		for(int i=0; i<length; i++){
			store[i] = listInfos[i].isSelecting();
		}
	}
	

}
class GridItemClickListener implements OnItemClickListener{

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		
		ChoiceAdapter ca = (ChoiceAdapter)parent.getAdapter();
//		ca.toggleSelect(position);
		ca.onItemClick(position);
	}
}

class ChoiceAdapter extends ArrayAdapter<ChoiceInfo>{
	private Context context;
	private ChoiceInfo []listInfo;
	private View []ticks;
	public ChoiceAdapter(Context context, ChoiceInfo []objects) {
		
		super(context, R.layout.setting_tab_general_choice_item, objects);
		this.context = context;
		this.listInfo = objects;
		ticks = new View[objects.length];
		for (int i = 0 ; i< objects.length ; i++)
		{
			
			if (objects[i].isSelecting())
			{
				tickItem(i);
			}
		}
		
	}
	public View getView(int position,  View convertView, ViewGroup parent){
		View cell = convertView;
		
		if (convertView == null){
			LayoutInflater inflater = ((Activity)context).getLayoutInflater();
			cell = inflater.inflate(R.layout.setting_tab_general_choice_item, null);
		}
		ImageView image = (ImageView) cell.findViewById(R.id.setting_tab_general_image);
		TextView text = (TextView) cell.findViewById(R.id.setting_tab_general_text);
		View tick = cell.findViewById(R.id.setting_tab_general_tick);
		text.setText(listInfo[position].getType());
		image.setImageDrawable(staticClass.getDrawable(position,context));
		if(listInfo[position].isSelecting())
			tick.setVisibility(View.VISIBLE);
		else
			tick.setVisibility(View.INVISIBLE);
		ticks[position] = tick;
		return cell;
			
	}
	public ChoiceInfo[] getListInfo() {
		return listInfo;
	}
	public void onItemClick(int position) {
		listInfo[position].toggle();
		if(listInfo[position].isSelecting())
			ticks[position].setVisibility(View.VISIBLE);
		else
			ticks[position].setVisibility(View.INVISIBLE);
		notifyDataSetChanged();
	}
	public void reset(boolean[] store) {
		int length = listInfo.length;
		for(int i = 0; i<length; i++){
			listInfo[i].setSelecting(store[i]);
			tickItem(i);
		}
		notifyDataSetChanged();
	}
	public void toggleAll() {
		int length = listInfo.length;
		for(int i=0; i<length; i++){
			listInfo[i].toggle();
			tickItem(i);
		}
		notifyDataSetChanged();
	}
	private void tickItem(int position) {
		if(ticks[position] != null)
			if(listInfo[position].isSelecting())
				ticks[position].setVisibility(View.VISIBLE);
			else
				ticks[position].setVisibility(View.INVISIBLE);
	}
	
	
	public void checkAll() {
		int length = listInfo.length;
		for(int i=0; i<length; i++){
			listInfo[i].setSelecting(true);
			tickItem(i);
		}
		notifyDataSetChanged();
	}
	public void checkNone() {
		int length = listInfo.length;
		for(int i=0; i<length; i++){
			listInfo[i].setSelecting(false);
			tickItem(i);
		}
		notifyDataSetChanged();
	}
	
	
}
class ChoiceInfo {
	private String type;
	private boolean isSelecting;
	public ChoiceInfo(String type, boolean isSelecting) {
		setType(type);
		setSelecting(isSelecting);
	}
	public ChoiceInfo(String type) {
		setType(type);
		setSelecting(false);
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getType() {
		return type;
	}
	public void setSelecting(boolean isSelecting) {
		this.isSelecting = isSelecting;
	}
	public boolean isSelecting() {
		return isSelecting;
	}
	public void toggle() {
		isSelecting = !isSelecting;
	}
}